package luj.tool.jnproxyan.proxy.network.https.keystore;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;

public class KeystoreFileReader {

  //> keytool -genkeypair -v -keyalg RSA -alias jnproxyan -dname "CN=localhost" -ext "SAN=DNS:localhost" -storepass abc123 -keystore ./key.p12
  //> keytool -export -keystore ./key.p12 -alias jnproxyan -file ./ca.cer

  //> keytool -genkeypair -v -keyalg RSA -alias ca -dname "CN=jnproxyan-ca" -storepass abc123 -keystore ./key.p12
  //> keytool -genkeypair -v -keyalg RSA -alias dynamic -dname "CN=jnproxyan" -storepass abc123 -keystore ./key.p12
  //> keytool -export -keystore ./key.p12 -alias ca -file ./ca.cer

  public KeyStore read() throws Exception {
    var path = Paths.get("D:\\work\\personal\\2021proxy\\jnproxyan\\jnproxyan\\dat\\sslj\\key.p12");

    try (var in = Files.newInputStream(path)) {
      KeyStore keystore = KeyStore.getInstance("PKCS12");
      keystore.load(in, "abc123".toCharArray());

      return keystore;
    }
  }
}
